﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using DevExpress.Utils.Extensions;
using DevExpress.XtraVerticalGrid.Rows;
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using XCI.Extensions;
using XCI.Helper;
using XCI.Windows;

namespace XCI.Setting.Controls
{
    public partial class SettingVGridRows : BaseUserControl
    {
        private VGridControlPlus vgrid;
        public SettingVGridRows()
        {
            InitializeComponent();
        }

        public void Init(VGridControlPlus vgrid)
        {
            if (vgrid == null) return;
            this.vgrid = vgrid;
            LoadData();
            SetStatus();
        }

        protected void LoadData()
        {
            listBoxControl1.Items.BeginUpdate();
            listBoxControl1.Items.Clear();
            for (var index = 0; index < vgrid.Rows.Count; index++)
            {
                var row = vgrid.Rows[index];
                var name = $"行{index + 1}";
                if (row.Properties != null && !string.IsNullOrWhiteSpace(row.Properties.Caption))
                {
                    name = $"{row.Properties.Caption}";
                    if (!string.IsNullOrWhiteSpace(row.Properties.FieldName))
                    {
                        name += $"-{row.Properties.FieldName}";
                    }
                }
                listBoxControl1.Items.Add(name);
            }
            listBoxControl1.Items.EndUpdate();
        }

        protected List<BaseRow> ToSummaryList()
        {
            var list = new List<BaseRow>(vgrid.Rows.Count);
            vgrid.Rows.ForEach(p => list.Add(p));
            return list;
        }

        protected void ToSummaryCollection(List<BaseRow> list)
        {
            vgrid.Rows.Clear();
            list.ForEach(p => vgrid.Rows.Add(p));
        }

        private void SetStatus()
        {
            var index = listBoxControl1.SelectedIndex;
            btnDelete.Enabled = index > -1;
            btnUp.Enabled = index > 0;
            btnDown.Enabled = index >= 0 && index < vgrid.Rows.Count - 1;
        }

        private void MoveItem(int index, bool isUp)
        {
            var list = ToSummaryList();
            if (isUp)
            {
                list.MoveUp(index);
            }
            else
            {
                list.MoveDown(index);
            }
            ToSummaryCollection(list);
            LoadData();
            listBoxControl1.SelectedIndex = isUp ? (index - 1) : (index + 1);
        }

        private void listBoxControl1_SelectedValueChanged(object sender, EventArgs e)
        {
            SetStatus();
            var index = listBoxControl1.SelectedIndex;
            if (index == -1) return;

            propertyGridPlus.SelectedObject = vgrid.Rows[index];
        }

        private void xciPropertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            if (e.ChangedItem.Label != null &&
                (e.ChangedItem.Label.Equals("Caption") || e.ChangedItem.Label.Equals("FieldName")))
            {
                LoadData();
            }
        }

        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var index = vgrid.Rows.Count;
            var row = new EditorRow();
            row.Name = $"row{index}";
            row.Visible = true;
            vgrid.Rows.Add(row);
            LoadData();
            listBoxControl1.SelectedIndex = vgrid.Rows.Count - 1;
        }

        private void BtnNewCategoryRow_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var index = vgrid.Rows.Count;
            var row = new CategoryRow();
            row.Name = $"row{index}";
            row.Visible = true;
            vgrid.Rows.Add(row);
            LoadData();
            listBoxControl1.SelectedIndex = vgrid.Rows.Count - 1;
        }

        private void BtnNewMultiEditorRow_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var index = vgrid.Rows.Count;
            var row = new MultiEditorRow();
            row.Name = $"row{index}";
            row.Visible = true;
            vgrid.Rows.Add(row);
            LoadData();
            listBoxControl1.SelectedIndex = vgrid.Rows.Count - 1;
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要删除选中项吗？") == DialogResult.Yes)
            {
                vgrid.Rows.RemoveAt(listBoxControl1.SelectedIndex);
                LoadData();
            }
        }

        private void btnUp_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            MoveItem(listBoxControl1.SelectedIndex, true);
        }

        private void btnDown_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            MoveItem(listBoxControl1.SelectedIndex, false);
        }

        private void btnAuto_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBoxHelper.ShowMessageYesNo("当前字段将被清除,确定自动生成字段吗？") != DialogResult.Yes) return;
            vgrid.AutoBuildColumn();
            LoadData();
            listBoxControl1.SelectedIndex = 0;
        }

    }
}
